﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Collections;
using System.Xml.Serialization;

namespace OrderManager
{
    public class OrderService
    {
        public List<Order> orders = new List<Order>();
        public List<Order> OrderInq = new List<Order>();
        public void AddOrder(Order order)
        {

            bool flag = true;

            foreach (Order item in orders)
            {
                flag = order.Equals(item);
                Console.WriteLine(flag);
                if (flag)
                {
                    Console.WriteLine("Fail added.Repeated order");
                    return;
                }
            }
            orders.Add(order);
            Console.WriteLine("Successfully Add Order");
        }

        //public void DelOrder(int choice, Order o)
        //{

        //    //bool flag = false;
        //    //Order ord;
        //    switch (choice)
        //    {
        //        case 1:
        //            {
        //                Console.WriteLine("Please input the order ID:");
        //                int id = Convert.ToInt32(Console.ReadLine());
        //                for (int i = 0; i < orders.Count; i++)
        //                    if (orders[i].Id == id)
        //                    {
        //                        Console.WriteLine(orders[i].ToString());
        //                        Console.WriteLine("Delete? y/n");
        //                        char c = Convert.ToChar(Console.ReadLine());
        //                        switch (c)
        //                        {
        //                            case 'y':
        //                                {
        //                                    orders.Remove(orders[i]);
        //                                    Console.WriteLine("successfully delete");
        //                                    goto END;
        //                                }
        //                            case 'n': goto END;
        //                            default: goto END;
        //                        }

        //                    }
        //                Console.WriteLine("order doen't exist");
        //                break;
        //            }
        //        case 2:
        //            {
        //                Console.WriteLine("Please input the customer name:");
        //                string name = Console.ReadLine();
        //                for (int i = 0; i < orders.Count; i++)
        //                    if (orders[i].customer.CustomerName == name)

        //                    {
        //                        Console.WriteLine(orders[i].ToString());
        //                        Console.WriteLine("Delete? y/n");
        //                        char c = Convert.ToChar(Console.ReadLine());
        //                        switch (c)
        //                        {
        //                            case 'y':
        //                                {
        //                                    orders.Remove(orders[i]);
        //                                    Console.WriteLine("successfully delete");
        //                                    goto END;

        //                                }
        //                            case 'n': goto END;
        //                            default: goto END;
        //                        }

        //                    }
        //                Console.WriteLine("order doen't exist");
        //                break;
        //            }
        //        case 3:
        //            {
        //                Console.WriteLine("Please input the price:");
        //                double price = Convert.ToDouble(Console.ReadLine());
        //                for (int i = 0; i < orders.Count; i++)
        //                    if (orders[i].stuff.TotalPrice == price)
        //                    {
        //                        Console.WriteLine(orders[i].ToString());
        //                        Console.WriteLine("Delete? y/n");
        //                        char c = Convert.ToChar(Console.ReadLine());
        //                        switch (c)
        //                        {
        //                            case 'y':
        //                                {
        //                                    orders.Remove(orders[i]);
        //                                    Console.WriteLine("successfully delete");
        //                                    goto END;

        //                                }
        //                            case 'n': goto END;
        //                            default: goto END;
        //                        }

        //                    }
        //                Console.WriteLine("order doen't exist");
        //                break;
        //            }
        //        case 4:
        //            {
        //                Console.WriteLine("Please input the stuff type:");
        //                string stutype = Console.ReadLine();
        //                for (int i = 0; i < orders.Count; i++)
        //                    if (orders[i].stuff.StuffType == stutype)
        //                    {
        //                        Console.WriteLine(orders[i].ToString());
        //                        Console.WriteLine("Delete? y/n");
        //                        char c = Convert.ToChar(Console.ReadLine());
        //                        switch (c)
        //                        {
        //                            case 'y':
        //                                {
        //                                    orders.Remove(orders[i]);
        //                                    Console.WriteLine("successfully delete");
        //                                    goto END;

        //                                }
        //                            case 'n': goto END;
        //                            default: goto END;
        //                        }

        //                    }
        //                Console.WriteLine("order doen't exist");
        //                break;
        //            }
        //        default: break;
        //    }
        //END: Console.ReadKey();
        //    Console.Clear();
        //}

        public void DelOrder(int id)
        {
            //bool flag = false;
            for (int i = 0; i < orders.Count(); i++)
                if (orders[i].Id == id)
                {
                    orders.Remove(orders[i]);
                    //flag = true;
                    Console.WriteLine("successfully delete");

                }
            //return flag;
        }
        public bool InqOrder(int id)
        {
            var query = from o in orders where o.Id == id orderby o.stuff.TotalPrice select o;
            OrderInq.Clear();
            OrderInq = query.ToList();
            if (OrderInq == null)
            {
                Console.WriteLine("Order doesn't exist");
                return false;
            }

            else
            {
                foreach (Order o in OrderInq)
                    Console.WriteLine(o.ToString());
                return true;
            }

        }
        public bool InqOrder(int choice, string NameOrType)
        {
            OrderInq.Clear();
            switch (choice)
            {
                case 2:
                    {
                        var query = from o in orders where o.customer.CustomerName == NameOrType orderby o.stuff.TotalPrice select o;

                        OrderInq = query.ToList();
                        if (OrderInq == null)
                        {
                            Console.WriteLine("Order doesn't exist");
                            return false;
                        }
                        else
                        {
                            foreach (Order o in OrderInq)
                                Console.WriteLine(o.ToString());
                            return true;
                        }
                    }
                case 3:
                    {
                        var query = from o in orders where o.stuff.StuffType == NameOrType orderby o.stuff.TotalPrice select o;
                        OrderInq = query.ToList();
                        if (OrderInq == null)
                        {
                            Console.WriteLine("Order doesn't exist");
                            return false;
                        }
                        else
                        {
                            foreach (Order o in OrderInq)
                                Console.WriteLine(o.ToString());
                            return true;
                        }
                    }
                default: return false;
            }
        }
        public bool InqOrder(double price)
        {
            OrderInq.Clear();
            var query = from o in orders where o.stuff.TotalPrice == price orderby o.stuff.TotalPrice select o;
            OrderInq = query.ToList();
            if (OrderInq == null)
            {
                Console.WriteLine("Order doesn't exist");
                return false;
            }
            else
            {
                foreach (Order o in OrderInq)
                    Console.WriteLine(o.ToString());
                return true;
            }
        }
        //public void EditOrder(int OrderId,)
        //{
        //    Console.WriteLine("Please input the order Id you want to edit");
        //    int ordID = Convert.ToInt32(Console.ReadLine());
        //    var query = from o in orders where o.Id == ordID select o;
        //    List<Order> ord = query.ToList();
        //    if (ord.Count() > 0)
        //    {

        //        Console.WriteLine("Find the target order:");
        //        foreach (Order o in ord)
        //            Console.WriteLine(o.ToString());
        //    }
        //    else
        //    {
        //        Console.WriteLine("Order does not exist");
        //        return;
        //    }


        //    Console.WriteLine("Please choose the edit item:");
        //    Console.WriteLine("1.order ID");
        //    Console.WriteLine("2.customer name");
        //    Console.WriteLine("3.stuff total price");
        //    Console.WriteLine("4.stuff type");
        //    int choice = Convert.ToInt32(Console.ReadLine());
        //    switch (choice)
        //    {
        //        case 1:
        //            {
        //                Console.WriteLine("Please input the updated order ID:");
        //                int id = Convert.ToInt32(Console.ReadLine());
        //                ord[0].Id = id;
        //                Console.WriteLine("updated order:" + ord[0].ToString());
        //                break;
        //            }
        //        case 2:
        //            {
        //                Console.WriteLine("Please input the customer name:");
        //                string name = Console.ReadLine();
        //                ord[0].customer.CustomerName = name;
        //                Console.WriteLine("updated order:" + ord[0].ToString());
        //                break;
        //            }
        //        case 3:
        //            {
        //                Console.WriteLine("Please input the price:");
        //                double price = Convert.ToDouble(Console.ReadLine());
        //                ord[0].stuff.TotalPrice = price;
        //                Console.WriteLine("updated order:" + ord[0].ToString());
        //                break;
        //            }
        //        case 4:
        //            {
        //                Console.WriteLine("Please input the stuff type:");
        //                string stutype = Console.ReadLine();
        //                ord[0].stuff.StuffType = stutype;
        //                Console.WriteLine("updated order:" + ord[0].ToString());
        //                break;
        //            }
        //        default: break;

        //    }
        //    Console.ReadKey();
        //    Console.Clear();
        //}
        public void EditOrder(Order o, int newID)
        {
            DelOrder(o.Id);
            Order temp = o;
            o.Id = newID;
            bool flag = false;
            foreach (Order item in orders)
            {
                flag = o.Equals(item);
                Console.WriteLine(flag);
                if (flag)
                {
                    Console.WriteLine("Fail Edit.Repeated order");
                    AddOrder(temp);
                    return;
                }
            }
            AddOrder(o);
            Console.WriteLine("updated order:" + o.ToString());
        }
        public void EditOrder(Order o, string NameorType, int choice)
        {
            DelOrder(o.Id);
            Order temp = o;
            switch (choice)
            {
                case 2:
                    {
                        o.customer.CustomerName = NameorType;
                        bool flag = false;
                        foreach (Order item in orders)
                        {
                            flag = o.Equals(item);
                            Console.WriteLine(flag);
                            if (flag)
                            {
                                Console.WriteLine("Fail Edit.Repeated order");
                                AddOrder(temp);
                                return;
                            }
                        }
                        AddOrder(o);
                        Console.WriteLine("updated order:" + o.ToString());
                        break;
                    }

                case 4:
                    {
                        o.stuff.StuffType = NameorType;

                        bool flag = false;
                        foreach (Order item in orders)
                        {
                            flag = o.Equals(item);
                            Console.WriteLine(flag);
                            if (flag)
                            {
                                Console.WriteLine("Fail Edit.Repeated order");
                                AddOrder(temp);
                                return;
                            }
                        }
                        AddOrder(o);
                        Console.WriteLine("updated order:" + o.ToString());
                        break;
                    }
                default: break;
            }
        }
        public void EditOrder(Order o, double newPrice)
        {
            DelOrder(o.Id);
            Order temp = o;
            o.stuff.TotalPrice = newPrice;
            bool flag = false;
            foreach (Order item in orders)
            {
                flag = o.Equals(item);
                Console.WriteLine(flag);
                if (flag)
                {
                    Console.WriteLine("Fail Edit.Repeated order");
                    AddOrder(temp);
                    return;
                }
            }
            AddOrder(o);
            Console.WriteLine("updated order:" + o.ToString());
        }

        public List<Order> SortOrder(int choice)
        {
            switch (choice)
            {
                case 0:
                    {
                        Console.WriteLine("orders sorted by order id:");
                        var query = from o in orders orderby o.Id select o;
                        //var query1 = from o in orders orderby o.stuff.TotalPrice select o;
                        List<Order> ord = query.ToList();
                        
                        foreach (Order order in ord)
                            Console.WriteLine(order.ToString());
                        return ord;
                    }
                case 1:
                    {
                        Console.WriteLine("orders sorted by total price");
                        var query1 = from o in orders orderby o.stuff.TotalPrice select o;
                        List<Order> ord1 = query1.ToList();
                        
                        foreach (Order order in ord1)
                            Console.WriteLine(order.ToString());
                        return ord1;
                    }
                default:return null;

            }
           
        }

        public bool Export()
        {
            var reader = new XmlSerializer(typeof(List<Order>));
            //写入文件
            using (var fs = File.OpenWrite(@"DataFile.xml"))
            {
                reader.Serialize(fs, orders);
            }
            //reader.Serialize(Console.Out, orders);
            Console.WriteLine("Successfully export");


            return true;
        }

        public bool Import()
        {
            var reader = new XmlSerializer(typeof(List<Order>));
            using (var fs = File.OpenRead(@"DataFile.xml"))
            {

                var result = reader.Deserialize(fs) as List<Order>;
                foreach (Order item in result)
                {
                    bool flag = true;
                    foreach (Order o in orders)
                    {
                        if (o.Equals(item))
                        {
                            flag = false;
                        }


                    }
                    if (flag)
                        orders.Add(item);
                }

                //result.ForEach(Console.WriteLine);
            }
            Console.WriteLine("Successfully import");
            return true;

        }
    }


}
